package com.bort.system;

import com.bort.system.common.CloseSystemActionListener;
import com.bort.system.common.DataContainer;
import com.bort.system.common.TrayIconActionListener;
import com.bort.system.scheduler.ClientRefreshScheduler;
import com.bort.system.serial.SerialManager;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.application.Platform;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.awt.*;

public class FXApplication extends Application {

    static FxViewLoader fxViewLoader;

    public static Stage primaryStage;

    public static HostServices hostServices;

    private static TrayIcon trayIcon;

    private static MenuItem menuOpen;

    private static MenuItem menuExit;

    private static TrayIconActionListener actionListener;

    private static CloseSystemActionListener closeSystemListener;

    private static ClientRefreshScheduler scheduler;

    @Override
    public void start(Stage stage) throws Exception {
        setTitleAndIcon(stage);
        primaryStage = stage;
        Parent view = fxViewLoader.loadView();
        Scene scene = new Scene(view);
        stage.setScene(scene);
        stage.show();
        initSystemTray();
        hostServices = getHostServices();
    }

    private static void setTitleAndIcon(Stage stage) {
        stage.setTitle("睿美台球管理系统");
        javafx.scene.image.Image icon = new javafx.scene.image.Image("img/logo-3.png");
        stage.getIcons().add(icon);
    }

    public static void launch(Class<? extends Application> appClass, Class<? extends FxViewLoader> viewClass, String... args) {
        try {
            fxViewLoader = viewClass.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        SerialManager.getInstance().init();
        scheduler = new ClientRefreshScheduler();
        scheduler.autoRefresh();
        scheduler.portStateMonitor();
        launch(args);
    }


    public static HostServices getAppHostServices() {
        return hostServices;
    }

    public static void showView(Class<? extends FxViewLoader> viewLoader) {
        try {
            fxViewLoader = viewLoader.newInstance();
            Stage stage = new Stage();
            setTitleAndIcon(stage);
            primaryStage = stage;
            Parent view = fxViewLoader.loadView();
            Scene scene = new Scene(view);
            stage.setScene(scene);
            stage.show();
            trayIcon.removeActionListener(actionListener);
            menuOpen.removeActionListener(actionListener);
            actionListener = new TrayIconActionListener(stage);
            trayIcon.addActionListener(actionListener);
            menuOpen.addActionListener(actionListener);
            menuExit.removeActionListener(closeSystemListener);
            closeSystemListener = new CloseSystemActionListener(stage,scheduler,trayIcon);
            menuExit.addActionListener(closeSystemListener);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static Stage getStage() {
        return primaryStage;
    }

    private static void initSystemTray(){
        try {
            Stage stage = BortApplication.getStage();
            if(SystemTray.isSupported()){
                Platform.setImplicitExit(false);
                SystemTray tray = SystemTray.getSystemTray();
                Image image = Toolkit.getDefaultToolkit().createImage(FXApplication.class.getClass().getResource("/img/logo-3.png"));
                image.getScaledInstance(64,64,Image.SCALE_SMOOTH);
                getTrayIcon(image, stage, tray);
                actionListener = new TrayIconActionListener(stage);
                trayIcon.addActionListener(actionListener);
                tray.add(trayIcon);
                DataContainer.trayIcon = trayIcon;
            }
        } catch (AWTException e){
            e.printStackTrace();
        }
    }

    private static TrayIcon getTrayIcon(Image image, Stage stage, SystemTray tray) {
        PopupMenu menus = new PopupMenu();
        menuOpen = new MenuItem("显示主界面");
        menuExit = new MenuItem("退出程序");
        menus.add(menuOpen);
        menus.add(menuExit);

        trayIcon = new TrayIcon(image,"台球厅管理系统",menus);
        trayIcon.setImageAutoSize(true);
        menuOpen.addActionListener(actionListener);
        closeSystemListener = new CloseSystemActionListener(stage, scheduler, trayIcon);
        menuExit.addActionListener(closeSystemListener);
        return trayIcon;
    }
}
